import myAxios from './myAxios'
import jsonp from 'jsonp'
import { message } from 'antd'
import { BASE_URL, WEATHER_AK, CITY } from '../config'

//登录请求
export const reqLogin = (username, password) => {
  //axios 的post请求默认将参数转成JSON格式发给服务器
  return myAxios.post(`${BASE_URL}/login`, { username, password })
}

//获取商品分类列表
export const reqCategoryList = () => myAxios.get(`${BASE_URL}/manage/category/list`)

//获取天气信息（百度接口）
export const reqWeather = () => {
  return new Promise((resolve, reject) => {
    jsonp(`http://api.map.baidu.com/telematics/v3/weather?location=${CITY}&output=json&ak=${WEATHER_AK}`, (err, data) => {
      if (err) {
        message.error('请求天气接口失败，请联系管理员')
        return new Promise(() => { })
      } else {
        resolve(data.results[0].weather_data[0])
      }
    })
  })
}

//新增商品分类
export const reqAddCategory = ({ categoryName }) => {
  //axios 的post请求默认将参数转成JSON格式发给服务器
  return myAxios.post(`${BASE_URL}/manage/category/add`, { categoryName })
}
